<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\BaseController;
use app\common\model\Comment as CommentModel;
use app\common\model\Room as RoomModel;
use think\Request;


class Comment extends BaseController
{
    //评论
    public function index(Request $request)
    {
        $id = $request->get("id");
        $room = RoomModel::find($id);
        return view('comment/index', [
            'id' => $id,
            'name' => $room['name'] ?? '',
        ]);
    }

    //提交评论
    public function save(Request $request)
    {
        if (empty(session('user'))) {
            $this->redirect('login');
        }
        $data = $request->post();
        $data['room_id'] = $data['id'];
        $data['user_id'] = session('user.id');
        $data['username'] = session('user.username');
        unset($data['id']);
        CommentModel::create($data);
        return view('comment/index', [
            'id' => 0,
            'name' => '',
        ]);
    }
}
